/**
 * Created by Administrator on 2015/1/18.
 */
/**
 * Created by Administrator on 2014/12/25.
 */

function Game(){
    this.map;//扫雷游戏的布局
    this.nodelist;//用于存放单元格的数组
    this.row;//行数
    this.col;//列数
    this.mineSum;//雷的总数
    this.playerFind;//玩家发现的雷数
    this.time//时间
    this.timer;//计时器
    this.state=1;//难度状态,1:

    this.des=dom("DIV",{id:"des"});//描述游戏进展，剩余几个雷
    this.countTime=dom("DIV",{id:"countTime"});//时间计时
    this.draw=dom("DIV",{id:"draw"});//画布局
    this.cheat=dom("DIV",{id:"cheat"});//作弊器

    this.ref={
        "#":"Images/BlueSquare.png",
        "*":"Images/MineRed.png",
        "@":"Images/NewFlag.png",
        "0":"Images/BlankSquare.png",
        "1":"Images/1.png",
        "2":"Images/2.png",
        "3":"Images/3.png",
        "4":"Images/4.png",
        "5":"Images/5.png",
        "6":"Images/6.png",
        "7":"Images/7.png",
        "8":"Images/8.png"
    };//用于对照图片的src

    this.addNodes();
    this.init();
    this.setListener();
}
//添加节点,只要添加一次
Game.prototype.addNodes=function(){

    document.body.appendChild(this.des);
    document.body.appendChild(this.countTime);
    document.body.appendChild(this.draw);
    document.body.appendChild(this.cheat);

}
//初始化布局
Game.prototype.init=function(){
    //初始化布局参数
    this.gameLevel();

    if(this.timer!=null){
        var game=this;
        window.clearTimeout(this.timer);
    }
    //每次初始化都有将各个div的数据清空
    this.des.innerHTML="";
    this.countTime.innerHTML="";
    this.cheat.innerHTML="";
    this.draw.innerHTML="";

    this.des.appendChild(document.createTextNode("You have "+this.mineSum+" ray to find out."));
    this.countTime.appendChild(document.createTextNode("Time:"+this.time+" seconds"));

    //nodelist是一个二维数组，用于存放生成的单元格对象
    this.nodelist=new Array();
    for(var i=0;i<this.row;i++){
        this.nodelist[i]=new Array();
        for(var j=0;j<this.col;j++){
            var img=document.createElement("img");
            img.id=i+"-"+j;
            img.src="Images/BlueSquare.png";
            this.draw.appendChild(img);
            this.nodelist[i][j] = new BombCell(img,0,false,false);
        }
        this.draw.appendChild(dom("BR",null));
    }
}
//设置作弊器
Game.prototype.setCheat=function(){

    this.cheat.innerHTML="";
    this.cheat.appendChild(dom("BR",null));
    for(var i=0;i<this.nodelist.length;i++){
        for(var j=0;j<this.nodelist[i].length;j++){
            var img=document.createElement("img");
            img.src=this.ref[this.map[i][j]];
            if(img.src==undefined){
                console.log(i+"-"+j);
            }
            this.cheat.appendChild(img);
        }
        this.cheat.appendChild(dom("BR",null));
    }

}
//随机生成雷
Game.prototype.GenerateMine=function(){

    //先初始化布局
    this.init();
    //开始计时，每隔1秒，执行this.startTime函数
    this.timer=window.setInterval( method(this,"startTime"),1000);
    //随机生成雷的位置
    for(var i=0;i<this.mineSum;i++){
        var x=parseInt(Math.random()*this.row);
        var y=parseInt(Math.random()*this.col);
        if(this.nodelist[x][y].cAround==0){
            this.map[x][y]="*";
            this.nodelist[x][y].cAround=-1;//如果是雷将它的cAround设为-1
        }else{
            i--;
        };
    }
    //表示八个方向
    var dir=[[-1, -1],[-1, 0],[-1, 1],[0, 1],[1, 1],[1, 0],[1, -1],[0, -1]];
    //修改雷八个方向位置的单元格的cAround属性
    for(var x=0;x<this.row;x++){
        for(var y=0;y<this.col;y++){
            if(this.nodelist[x][y].cAround==-1){
                for(var k=0;k<dir.length;k++){
                    var r=x+dir[k][0];
                    var c=y+dir[k][1];
                    if((r>=0&&r<this.nodelist.length)&&(c>=0&&c<this.nodelist[x].length)){
                        if(this.nodelist[r][c].cAround!=-1){
                            this.nodelist[r][c].cAround=this.nodelist[r][c].cAround+1;
                            this.map[r][c]=""+this.nodelist[r][c].cAround;
                        }
                    }
                }
            }
        }
    }
    // 打开作弊器
    this.setCheat();

    //设置单元格的事件监听,以下是比较原始的方法，老师推荐使用冒泡的方法
    var game=this;
    for(var i=0;i<this.nodelist.length;i++){
        for(var j=0;j<this.nodelist[i].length;j++){
            //循环遍历为没一个单元格的图片对象，设置左键事件
            registerEventHandler( this.nodelist[i][j].pic,"click",function(e){
                var index= e.target.id.indexOf("-");
                game.onLeftClick(e.target.id.substring(0,index), e.target.id.substring(index+1));
            });
            //循环遍历为没一个单元格的图片对象，设置右键事件
            this.nodelist[i][j].pic.oncontextmenu=function(e){
                e.preventDefault();//禁止默认的右键事件
                var index= e.target.id.indexOf("-");
                game.onRightClick(e.target.id.substring(0,index), e.target.id.substring(index+1));
            };
        }
    }

}
//设置startGame,resetLevel按钮的事件
Game.prototype.setListener=function(){

    registerEventHandler(startGame,"click",this.GenerateMine.bind(this));
    registerEventHandler(resetLevel,"click",this.selectLevel.bind(this));

};
//鼠标左击事件
Game.prototype.onLeftClick=function(i,j){

    //判断失败
    if(this.nodelist[i][j].cAround==-1){
        var img=document.getElementById(i+"-"+j);
        img.src=this.ref[this.map[i][j]];
        this.nodelist[i][j].pic=img;
        alert("You failed!!!");
        this.des.innerHTML="";
        this.draw.innerHTML="";
        //停止计时
        var game=this;
        window.clearTimeout(game.timer);
        //显示结果
        this.setCheat();

    }

    //点出数字
    if(this.nodelist[i][j].cAround>0){
        var img=document.getElementById(i+"-"+j);
        img.src=this.ref[this.map[i][j]];
        this.nodelist[i][j].pic=img;
        this.nodelist[i][j].isUsed=true;
    }

    //判断成功
    var count=0;
    for(var a=0;a<this.row;a++){
        for(var b=0;b<this.col;b++){
            if(this.nodelist[a][b].isUsed==false){
                count++;
            }
        }
    }
    if(count==this.mineSum){

        alert("You win!!!");
        this.des.innerHTML="";
        this.draw.innerHTML="";
        //停止计时
        var game=this;
        window.clearTimeout(game.timer);
        //显示结果
        this.setCheat();
    }

    //如果是空白的单元格，则递归
    if(this.nodelist[i][j].cAround==0){

        this.openWhites(i,j);
    }
}
Game.prototype.openWhites=function(x,y){

    //如果越界，则返回
    if(x<0||y<0||x>=parseInt(this.row)||y>=parseInt(this.col)||this.nodelist[x][y].cAround==-1){
        return;
    }
    //如果是数字，则先显示再返回
    if(this.nodelist[x][y].cAround>0){
        var img=document.getElementById(x+"-"+y);
        img.src=this.ref[this.map[x][y]];
        this.nodelist[x][y].pic=img;
        this.nodelist[x][y].isUsed=true;
        return;
    }
    //如果是空白单元格且为被点开，则继续递归
    if(this.nodelist[x][y].cAround==0&&this.nodelist[x][y].isUsed==false){
        var img=document.getElementById(x+"-"+y);
        img.src=this.ref[this.map[x][y]];
        this.nodelist[x][y].pic=img;
        this.nodelist[x][y].isUsed=true;

        var dir=[
            [-1,-1],[0,-1],[1,-1],
            [-1, 0],       [1, 0],
            [-1, 1],[0, 1],[1, 1]];
        for(var k=0;k<dir.length;k++) {
            var i = x - dir[k][0];
            var j = y - dir[k][1];
            this.openWhites(i,j);
        }
    }

}
//鼠标右击事件
Game.prototype.onRightClick=function(i,j){

    //如果未标记，则将此单元格标记起来,再次点击还原成初始化
    if(this.nodelist[i][j].isFlag==true){
        this.playerFind--;
        var img=document.getElementById(i+"-"+j);
        img.src="Images/BlueSquare.png";
        this.nodelist[i][j].pic=img;
        this.nodelist[i][j].isFlag=false;

    }else if(this.nodelist[i][j].isUsed==false&&this.nodelist[i][j].isFlag==false){
        this.playerFind++;
        var img=document.getElementById(i+"-"+j);
        img.src="Images/NewFlag.png";
        this.nodelist[i][j].pic=img;
        this.nodelist[i][j].isFlag=true;
    }
    //右键点击之后，更新des信息,剩余雷数必须大于等于0
    this.des.innerHTML="";
    var remain=this.mineSum-this.playerFind;
    if(remain<0){
        remain=0;
    }
    var tex=document.createTextNode("You have "+remain+" ray to find out.");
    this.des.appendChild(tex);

}
//计时开始
Game.prototype.startTime=function(){

    this.countTime.innerHTML="";
    this.time++;
    this.countTime.appendChild(document.createTextNode("Time:"+this.time+" seconds"));

}
//reset level按钮设置
Game.prototype.selectLevel=function(){

    this.countTime.innerHTML="";
    if(this.timer!=null){
        var game=this;
        window.clearTimeout(this.timer);
    }
    var game=this;
    document.getElementById("setDegree").style.visibility="visible";
    var save=document.getElementById("save");
    save.onclick=function(){
        var radios=document.getElementsByName("levels");
        for(var i=0;i<radios.length;i++){
            if(radios[i].checked==true)
            {
                game.state=radios[i].value;
                document.getElementById("setDegree").style.visibility="hidden";
                game.init();
            }
        }
    };
}
//设置游戏难度
Game.prototype.gameLevel=function(){
    this.playerFind=0;
    if(this.state==1){
        this.row=9;
        this.col=9;
        this.mineSum=10;
    }else if(this.state==2){
        this.row=16;
        this.col=16;
        this.mineSum=40;
    }else if(this.state==3){
        this.row=16;
        this.col=30;
        this.mineSum=99;
    }else if(this.state==4){
        this.row=16;
        this.col=30;
        this.mineSum=150;
    }else if(this.state==5){
        this.row=document.getElementById("txtRows").value;
        this.col=document.getElementById("txtCols").value;
        this.mineSum=document.getElementById("txtBombs").value;
    }
    this.time=0;
    this.playerFind=0;
    this.map=new Array();
    for(var i=0;i<this.row;i++){
        this.map[i]=new Array();
        for(var j=0;j<this.col;j++){
            this.map[i][j]="0";
        }
    }
}










